﻿using MvcSimplyCleverPart3.Models;
using SimplyCleverMiddleTier.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcSimplyCleverPart3.Code
{
    public static  class Conversion
    {
        public static Address Cast(this AddressViewModel viewAddress)
        {
            var address = new Address();
            address.Line1 = viewAddress.Line1;
            address.Line2 = viewAddress.Line2;
            address.County = viewAddress.County;
            address.City = viewAddress.City;
            address.ZipCode = viewAddress.ZipCode;
            address.StateCode = viewAddress.StateCode;
            address.CountryCode = viewAddress.CountryCode;

            return address;
        }

        public static AddressViewModel Cast(this Address address)
        {
            var modelAddress = new AddressViewModel();
            modelAddress.Line1 = address.Line1;
            modelAddress.Line2 = address.Line2;
            modelAddress.County = address.County;
            modelAddress.City = address.City;
            modelAddress.ZipCode = address.ZipCode;
            modelAddress.StateCode = address.StateCode;
            modelAddress.CountryCode = address.CountryCode;

            return modelAddress;
        }

        public static UserAddressViewModel Cast(this UserAddress userAddress)
        {
            var modelUserAddress = new UserAddressViewModel();
            modelUserAddress.Address = userAddress.Address.Cast();
            modelUserAddress.LastModified = userAddress.LastModified;

            return modelUserAddress;
        }

        public static UserAddressViewModel[] Cast(this UserAddress[] list)
        {
            UserAddressViewModel[] modelList = new UserAddressViewModel[list.Length];
            for(int i=0; i< list.Length; i++)
                modelList[i] = list[i].Cast();
            
            return modelList;
        }
    }
}